//
//  DurationGoalSetupItemView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/12.
//

import Foundation
import MYModule


class GoalSetupDurationItemView: GoalSetupNormalItemView {
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(self.hourPickerAndUnitView)
        self.hourPickerAndUnitView.snp.makeConstraints { make in
            make.centerY.equalTo(self.pickerAndUnitView.snp.centerY)
            make.trailing.equalTo(self.pickerAndUnitView.snp.leading)
        }
    }
    
    override func bindModel(model: GoalSetupNormalItemModel) {
        super.bindModel(model: model)
        self.hourPickerAndUnitView.unitLabel.text = model.hourPickerModel.unit
        self.hourPickerAndUnitView.pickerView.titles = model.hourPickerModel.pickerDataArray
        self.hourPickerAndUnitView.pickerView.didSelectItem(atIndex: model.hourPickerModel.selectIndex)
        self.hourPickerAndUnitView.updatePickerFrame(_withWidth: model.hourPickerModel.pickerWidth)
        self.hourPickerAndUnitView.pickerView.returnDidSelectClosure { value in
            model.hourPickerModel.selectIndex = value
            if value == 0 && model.pickerModel.selectIndex < 10 {
                model.pickerModel.selectIndex = 10
                self.pickerAndUnitView.pickerView.didSelectItem(atIndex: 10)
            }
        }
        
        self.pickerAndUnitView.pickerView.returnDidSelectClosure { value in
            
            guard model.hourPickerModel.selectIndex == 0 && value < 10 else {
                model.pickerModel.selectIndex = value
                return
            }
            model.pickerModel.selectIndex = 10
            self.pickerAndUnitView.pickerView.didSelectItem(atIndex: 10)
        }
    }
    
    //MARK: - lazy load
    public lazy var hourPickerAndUnitView: GoalSetupPickerAndUnitView = {
        let pickerView = GoalSetupPickerAndUnitView()
        return pickerView
    }()
    
}
